package com.didd.core.common.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.didd.core.common.utils.BeanUtil;
import lombok.*;

import java.io.Serializable;
import java.util.List;

/**
 * @author diddd
 * @date 2019/07/15
 */
@Getter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class SimplePage<T> implements Serializable {

    /**
     * 总条数
     */
    private Long count;
    /**
     * 分页list对象
     */
    private List<T> data;

    SimplePage(Builder<T> builder) {
        this.count = builder.count;
        this.data = BeanUtil.copyList(builder.data, builder.target);
    }

    public static <T> Builder<T> builder(Class<T> target) {
        return new Builder<>(0L, Lists.newArrayList(), target);
    }

    @Data
    @AllArgsConstructor
    public static class Builder<T> {
        /**
         * 总条数
         */
        private Long count;
        /**
         * 分页list对象
         */
        private List<?> data;
        /**
         * 目标对象
         */
        private Class<T> target;

        public Builder<T> count(Long count) {
            this.count = count;
            return this;
        }

        public Builder<T> data(List<?> data) {
            this.data = data;
            return this;
        }

        public Builder<T> page(IPage page) {
            this.data = page.getRecords();
            this.count = page.getTotal();
            return this;
        }

        public SimplePage<T> build() {
            return new SimplePage<>(this);
        }
    }
}
